package org.example.demo.Controller;

import org.example.demo.APIClass.User;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api")
public class HelloController {
    @GetMapping("/hello")
    public String hello() {
        return "Hello World";
    }

    // POST 请求
    @PostMapping("/adduser")
    public String addUser(@RequestBody User user) {
        System.out.println(user.toString());
        return "添加用户成功";
    }

    // Delete 请求
    @DeleteMapping("/delete_user/{id}")
    public String deleteUser(@PathVariable("id") int id) {
        System.out.println("删除用户id为：" + id);
        return "删除用户成功";
    }

    // PUT 请求
    @PutMapping("/update_user/")
    public String updateUser(@RequestBody User user) {
        System.out.println(user.toString());
        return "更新用户成功";
    }

    // GET 请求
    @GetMapping("/get_user/{id}")
    public String getUser(@PathVariable("id") int id) {
        System.out.println("获取用户id为：" + id);
        return "获取用户成功";
    }

    // Get请求
    @GetMapping("/get_user")
    public String getUserList() {
        System.out.println("获取用户列表");
        return "获取用户列表成功";
    }

    // 返回index.html页面

    @GetMapping("/index")

    public String index() {
        return "index";
    }

    // @CrossOrigin注解解决跨域问题

    @CrossOrigin(value = "http://localhost:8080", maxAge = 3600, allowedHeaders = "*")
    @GetMapping("/GetCORS")
    public String GetCORS() {
        return "GET请求跨域成功";
    }


}

